/**
 * 
 */
package com.easyidea.its.domain.service.user.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.domain.model.system.User;
import com.easyidea.its.domain.repository.user.UserRepository;
import com.easyidea.its.domain.service.base.impl.BaseServiceImpl;
import com.easyidea.its.domain.service.department.PassPortService;
import com.easyidea.its.domain.service.system.PermissionService;
import com.easyidea.its.domain.service.user.UserService;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.web.dto.UserDto;

/**
 * @author Administrator
 *
 */
public class UserServiceImpl extends BaseServiceImpl<User> implements
		UserService {
	UserRepository userRepository;	
	private PermissionService permissionService;
	private PassPortService passPortService; 
	public PassPortService getPassPortService() {
		return passPortService;
	}
	public void setPassPortService(PassPortService passPortService) {
		this.passPortService = passPortService;
	}
	public PermissionService getPermissionService() {
		return permissionService;
	}
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	public void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}
	@Override
	public UserRepository getBaseRepository() {
		return userRepository;
	}
	/* (non-Javadoc)
	 * @see com.easyidea.its.domain.service.user.UserService#getCountOfUser(java.lang.String, java.lang.String)
	 */
	public int getCountOfUser(String userName, String name) {
		// TODO Auto-generated method stub
		return userRepository.getCountOfUser(userName, name);
	}

	/* (non-Javadoc)
	 * @see com.easyidea.its.domain.service.user.UserService#getUsers(java.lang.String, java.lang.String, int, int)
	 */
	public Collection<User> getUsers(String name, String userName,
			int startAtIndex, int count) {
		// TODO Auto-generated method stub
		return userRepository.getUsers(name, userName, startAtIndex, count);
	}

	/* (non-Javadoc)
	 * @see com.easyidea.its.domain.service.user.UserService#hasUser(java.lang.Integer, java.lang.String)
	 */
	public boolean hasUser(Integer id, String userName) {
		// TODO Auto-generated method stub
		return userRepository.isRepeated("userName", userName, id, false);
	}

	/* (non-Javadoc)
	 * @see com.easyidea.its.domain.service.user.UserService#loginCheck(java.lang.String, java.lang.String)
	 */
	public User loginCheck(String userName, String password) {
		ItsConstants.setAllPermissionUrlList(permissionService.getAllPermissionUrls());
		return userRepository.loginCheck(userName, password);
	}
	public void saveUserPassPort(Integer userId, Integer[] passPortId) {
     User u = this.getBeanById(userId);
     u.setPassPortList(null);
     if(passPortId != null && passPortId.length > 0) {
	     List<PassPort> pps = new ArrayList<PassPort>();
	     for (int i =0;i<passPortId.length;i++) {
	    	 pps.add(this.passPortService.getBeanById(passPortId[i])); 
	     }
	     u.setPassPortList(pps);
     }
     this.update(u);
	}

	public UserRepository getUserRepository() {
		return userRepository;
	}
	public int getUserCountByUserDto(UserDto userDto) {
		return userRepository.getUserCountByUserDto(userDto);
	}
	public Collection<User> getUsersByUserDto(UserDto userDto, int start, int pageSize) {
		Collection<User> users = userRepository.getUsersByUserDto(userDto, start, pageSize);
		return users;
	}
	@SuppressWarnings("unchecked")
	public User getUserPassPortsByUserId(Integer userId) {
		User user = this.getBeanById(userId);
		Collection<Integer> ps =  this.userRepository.executePureSqlCol(
				"select passport_id from userandpassport uap,PassPort pp where " +
				"pp.is_deleted = 0 and pp.id = uap.passport_id and user_id = " + userId);
		List<PassPort> passPortList  = new ArrayList<PassPort>();
		for(Integer pId : ps) {
			passPortList.add(passPortService.getBeanById(pId));
		}
		user.setPassPortList(passPortList);
		return user;
	}
}
